/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.sedona.core.spatialRDD;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.sedona.common.enums.FileDataSplitter;
import org.apache.sedona.core.TestBase;
import org.apache.sedona.core.enums.GridType;
import org.apache.sedona.core.enums.IndexType;
import org.locationtech.jts.geom.Envelope;

public class SpatialRDDTestBase extends TestBase {
  protected static long inputCount;
  protected static Envelope inputBoundary;
  /** The prop. */
  static Properties prop;
  /** The input. */
  static InputStream input;
  /** The Input location. */
  static String InputLocation;

  /** The offset. */
  static Integer offset;

  /** The splitter. */
  static FileDataSplitter splitter;

  /** The grid type. */
  static GridType gridType;

  /** The index type. */
  static IndexType indexType;

  /** The num partitions. */
  static Integer numPartitions;

  /** Once executed before all. */
  protected static void initialize(final String testSuiteName, final String propertiesFileName) {
    TestBase.initialize(testSuiteName);

    prop = new Properties();
    ClassLoader classLoader = SpatialRDDTestBase.class.getClassLoader();
    input = classLoader.getResourceAsStream(propertiesFileName);

    offset = 0;
    splitter = null;
    gridType = null;
    indexType = null;
    numPartitions = 0;

    try {
      // load a properties file
      prop.load(input);
      // There is a field in the property file, you can edit your own file location there.
      // InputLocation = prop.getProperty("inputLocation");
      InputLocation =
          "file://" + classLoader.getResource(prop.getProperty("inputLocation")).getPath();
      inputCount = Long.parseLong(prop.getProperty("inputCount"));
      String[] coordinates = prop.getProperty("inputBoundary").split(",");
      inputBoundary =
          new Envelope(
              Double.parseDouble(coordinates[0]),
              Double.parseDouble(coordinates[1]),
              Double.parseDouble(coordinates[2]),
              Double.parseDouble(coordinates[3]));
      offset = Integer.parseInt(prop.getProperty("offset"));
      splitter = FileDataSplitter.getFileDataSplitter(prop.getProperty("splitter"));
      gridType = GridType.getGridType(prop.getProperty("gridType"));
      indexType = IndexType.getIndexType(prop.getProperty("indexType"));
      numPartitions = Integer.parseInt(prop.getProperty("numPartitions"));
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      if (input != null) {
        try {
          input.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}
